
//express的配置文件

var express = require('express');

var pkg = require('../package.json');

var flash = require('connect-flash');

var compress = require('compression');

var serveStatic = require('serve-static');

var bodyParser  = require('body-parser');

var methodOverride = require('method-override');

var cookieParser = require('cookie-parser');

var env 		= process.env.NODE_ENV || 'development';

var log4js 		= require('log4js');
var logger 		= require('./logConfig').logger;

var query       = require('connect-query');

module.exports = function (app, config){

    //设置压缩
	app.use(compress({
    	filter: function(req, res) {
      		return /json|text|javascript|css/.test(res.getHeader('Content-Type'));
   	 	},
    	level: 9
  	}));

	//设置静态目录
	//后期要考虑将静态目录从nodejs移除，采用反向代理到nginx来实现静态目录
  	app.use(serveStatic(config.root + '/public'));

    app.set('images', config.root+'/public/images');

  	//设置view engine，实际上并没有使用到web页面渲染，可考虑去掉
  	//设置本地变量
  	app.use(function (req, res, next) {
      	res.locals.pkg = pkg;
      	res.locals.env = env;
      	next();
    });

  	//配置log4js日志组件
  	app.use(log4js.connectLogger(logger, {
      	level: 'auto',
      	format:':method :url'
    }));

  	app.use(bodyParser.json());
  	app.use(bodyParser.urlencoded());

    app.use(query());

    app.use(methodOverride());

    app.use(cookieParser());

    app.use(flash());

    app.use(function (err, req, res, next) {
      if (err.timeout) {
        res.send({
          status: 1,
          error: 'Request Timeout'
        });
      }
    });


}